package com.game.chinesechess.piece.specific;

import com.game.chinesechess.piece.chess;
import com.game.chinesechess.util.Point;

public class cannonChess extends chess {
    public cannonChess(chess.Color color, int x, int y) {
        this.color = color;
        this.point = new Point(x, y);
    }

    @Override
    public Boolean check(chess[][] map, int toX, int toY) {
        int xDistance = this.point.x - toX;
        int yDistance = this.point.y - toY;
        int count = 0;
        if (!((xDistance == 0 && yDistance != 0) || (xDistance != 0 && yDistance == 0)))
            return false;
        if (xDistance == 0) {
            int begin, end;
            begin=this.point.y<toY?this.point.y+1:toY+1;
            end=this.point.y>toY?this.point.y:toY;
            for (int i = begin; i < end; i++)
                if (map[this.point.x][i] != null)
                    count++;
            if (map[toX][toY] == null && count == 0)
                return true;
            if (count == 1){
                if(map[toX][toY]==null)
                    return  false;
                else
                    if(map[toX][toY].color!=map[this.point.x][this.point.y].color)
                        return  true;
            }
        } else {
            int begin, end;
            begin = this.point.x < toX ? this.point.x+1 : toX+1;
            end = this.point.x > toX ? this.point.x : toX;
            for (int i = begin; i < end; i++)
                if (map[i][this.point.y] != null)
                    count++;
            if (map[toX][toY] == null && count == 0)
                return true;
            if (count == 1){
                if (map[toX][toY] == null)
                    return false;
                else
                    if (map[toX][toY].color != map[this.point.x][this.point.y].color)
                        return true;
            }
        }
        return false;
    }

    @Override
    public String getName() {
        return "炮";
    }
}